package com.future.mayi.qsale.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.future.baselib.utils.CommonUtils;
import com.future.mayi.R;
import com.future.mayi.adapter.BaseRecyclerViewAdapter;
import com.future.mayi.qsale.model.RedPacketInfoModel;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/6/21.
 */

public class SignatureAdapter extends BaseRecyclerViewAdapter<RedPacketInfoModel.RedpackListBean> {

    private int itemMargin;

    public SignatureAdapter(Context context, List<RedPacketInfoModel.RedpackListBean> list) {
        super(context, list);
        itemMargin = (CommonUtils.getScreenSize(context)[0] - 5 * (CommonUtils.dip2px(context, 45))) / 10;
        if (itemMargin <= 0) {
            itemMargin = CommonUtils.dip2px(context, 5);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new SignatureViewHolder(getView(parent, R.layout.item_signature));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        RedPacketInfoModel.RedpackListBean signatureModel = mList.get(position);
        if (signatureModel == null) return;
        setItemValues((SignatureViewHolder) holder, signatureModel, position);


    }

    public void setItemValues(SignatureViewHolder signatureViewHolder, RedPacketInfoModel.RedpackListBean signatureModel, int position) {

//        if (signatureModel.getType() == SignatureModel.TYPE_LOTTERY) {
//            signatureViewHolder.tvAmount.setText("抽奖");
//        } else if (signatureModel.getType() == SignatureModel.TYPE_RED_PACKET) {
//            signatureViewHolder.tvAmount.setText(getFormatPrice(signatureModel.getAmount()) + "元");
//        }

        ViewGroup.MarginLayoutParams layoutParams = new ViewGroup.MarginLayoutParams(signatureViewHolder.itemView.getLayoutParams());
        layoutParams.leftMargin = itemMargin;
        layoutParams.rightMargin = itemMargin;
        layoutParams.topMargin = (CommonUtils.dip2px(mContext, 25));
        layoutParams.bottomMargin = (CommonUtils.dip2px(mContext, 21));

        signatureViewHolder.itemView.setLayoutParams(layoutParams);

        signatureViewHolder.tvIndexDays.setText(mContext.getResources().getString(R.string.text_index_of_signature, signatureModel.getDay()));

        if (!signatureModel.isHasSignature()) {
            signatureViewHolder.ivRedPacketBfg.setImageResource(R.drawable.img_red_packet_opened);
        } else {
            signatureViewHolder.ivRedPacketBfg.setImageResource(R.drawable.img_red_packet_unopen);
        }

        if (position == (mList.size() - 1)) {
            signatureViewHolder.tvAmount.setText("抽奖");
            signatureViewHolder.ivRedPacketBfg.setImageResource(R.drawable.img_red_packet_unopen);
        } else {
            signatureViewHolder.tvAmount.setText(getFormatPrice(signatureModel.getPar_value()) + "元");
        }
    }


    private String getFormatPrice(double d) {
        String ds = null;
        DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance();
        df.setMaximumFractionDigits(0); //设置最多小数位数
        df.setMinimumFractionDigits(0); //设置最少小数位数
        ds = df.format(d);

        return ds;
    }


    public static class SignatureViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_red_packet_bfg)
        ImageView ivRedPacketBfg;
        @BindView(R.id.tv_amount)
        TextView tvAmount;
        @BindView(R.id.rl_red_packet)
        RelativeLayout rlRedPacket;
        @BindView(R.id.tv_index_days)
        TextView tvIndexDays;

        public SignatureViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
